home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / BUS / TMCM Software and Labs.sit / Software for TMCM 7_95 / Files for Lab 5 / Multiply by Adding < prev    next >
Text File  |  1994-04-16  |  2KB  |  51 lines

  1. ; A rather silly way of multiplying two numbers is by
  2. ; adding over and over.  For example, 13 times 7 can
  3. ; be computed as:  13 + 13 + 13 + 13 + 13 + 13 + 13.
  4. ; This program multiplies two numbers, N1 and N2, by
  5. ; adding N1 to itself N1 times.  The numbers to be
  6. ; multiplied should be put in memory locations N1 and N2
  7. ; before the program is run.  The answer will be in memory
  8. ; location Ans when the program halts.  (If you watch this
  9. ; program run, the locations to watch are Count and Ans,
  10. ; that is, locations 14 and 15.)
  11.  
  12. @PC 0  ; This is for convenience: it puts a 0 in the program
  13.        ; counter when this file is loaded.
  14.  
  15.         Lod-c 0     ; Start with a zero in locations Ans and
  16.         Sto Ans     ;   Count.  Ans will contain the product
  17.         Sto Count   ;   of N1 and N2.  Count is used to count
  18.                     ;   how many times N1 has been added to
  19.                     ;   itself.
  20.  
  21. Loop:   Lod Count   ; If Count is equal to N2, then N1 has
  22.         Sub N2      ;    been added to itself N2 times, and
  23.         Jmz Done    ;    the program is done.
  24.  
  25.         Lod Count   ; Add 1 to Count
  26.         Inc
  27.         Sto Count
  28.  
  29.         Lod Ans     ; Ans contains N1 + N1 + ...;  Add in 
  30.         Add N1      ;   another copy of N1 to Ans
  31.         Sto Ans
  32.  
  33.         Jmp Loop    ; Return to start of loop
  34.  
  35. Done:   Hlt
  36.  
  37. Count:  data        ; "data" is not an assembly-language
  38.                     ; instruction.  This tells the assembler
  39.                     ; to set aside a memory location to hold
  40.                     ; some as yet unknown data.  I use this
  41.                     ; so that I can give that location the name
  42.                     ; Count.  (Actually, the assembler puts
  43.                     ; a zero into that location, so that "data"
  44.                     ; is effectively the same as the number 0.)
  45.  
  46. Ans:    data
  47.  
  48. N1:     13          ; Numbers to be multiplied
  49. N2:     7
  50.  
  51.